package com.ruoyi.weixin.controller;

import java.util.List;

import com.ruoyi.weixin.domain.Grade;
import com.ruoyi.weixin.service.IGradeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weixin.domain.Client;
import com.ruoyi.weixin.service.IClientService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author cloudoer
 * @date 2019-09-03
 */
@Controller
@RequestMapping("/weixin/client")
public class ClientController extends BaseController
{
    private String prefix = "weixin/client";

    @Autowired
    private IClientService clientService;

    @Autowired
    private IGradeService gradeService;

    @RequiresPermissions("weixin:client:view")
    @GetMapping()
    public String client()
    {
        return prefix + "/client";
    }

    /**
     * 查询客户列表
     */
    @RequiresPermissions("weixin:client:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Client client)
    {
        startPage();
        List<Client> list = clientService.selectClientList(client);
        return getDataTable(list);
    }

    /**
     * 查询客户，包含地址信息
     */
    @RequiresPermissions("weixin:client:info")
    @PostMapping("/info")
    @ResponseBody
    public Client info(Client client){
        Client client1 = clientService.selectClientInfoById(client);
        return client1;
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("weixin:client:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Client client)
    {
        List<Client> list = clientService.selectClientList(client);
        ExcelUtil<Client> util = new ExcelUtil<Client>(Client.class);
        return util.exportExcel(list, "client");
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("weixin:client:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Client client)
    {
        return toAjax(clientService.insertClient(client));
    }

    /**
     * 修改客户
     */
    @GetMapping("/edit/{clientId}")
    public String edit(@PathVariable("clientId") String clientId, ModelMap mmap)
    {
        Client client1 = new Client();
        client1.setClientId(clientId);
        Client client = clientService.selectClientInfoById(client1);
        List<Grade> grades = gradeService.selectGradeList(null);
        mmap.put("grades", grades);
        mmap.put("client", client);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("weixin:client:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Client client)
    {
        return toAjax(clientService.updateClient(client));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("weixin:client:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(clientService.deleteClientByIds(ids));
    }
}
